import api from '../../utils/api';

Page({
  data: {
    img: '../../static/image/yinyuefengmian.jpg',
    popupShow: false,
    musicList: [],
    poster: '',
    name: '',
    author: '',
    src: '',
  },
  onLoad: function (options) {
    this.audioCtx = wx.createAudioContext('myAudio');
    this.getMusicList();
  },

  // 请求音乐列表
  getMusicList() {
    const _this = this;
    wx.showLoading({
      title: '请稍后...'
    });
    api({
      url: '/User/queryMusicList',
      method: 'GET',
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          musicList: res.data.data
        });
      }
    });
  },

  toMusic(e) {
    const { item } = e.currentTarget.dataset;
    this.setData({
      poster: item.music_image,
      name: item.music_name,
      author: item.music_author,
      src: item.music_path,
      popupShow: true
    });
  },

  onClose() {
    this.audioCtx.pause();
    this.setData({
      popupShow: false
    });
  },

  audioPlay: function () {
    this.audioCtx.play();
  },

  audioPause: function () {
    this.audioCtx.pause();
  }
})